package com.tianyadu.security.core.validate.code.sms;

import com.tianyadu.security.core.properties.SecurityProperties;
import com.tianyadu.security.core.validate.code.ValidateCode;
import com.tianyadu.security.core.validate.code.service.ValidateCodeGenerator;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @FileName: ImageCodeGenerator
 * @Description: 默认的图片验证码生成器
 * @author: xueyj
 * @create: 2018-12-11 15:59
 */
@Component("smsValidateCodeGenerator")
public class SmsCodeGenerator implements ValidateCodeGenerator {

    /** 获取Security配置属性 */
    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 生成校验码
     *@see com.tianyadu.security.core.validate.code.service.ValidateCodeGenerator#generate(ServletWebRequest)
     * @param request
     * @return
     */
    @Override
    public ValidateCode generate(ServletWebRequest request) {
        String code = RandomStringUtils.randomNumeric(securityProperties.getCode().getSms().getLength());
        return new ValidateCode(code, securityProperties.getCode().getSms().getExpireIn());
    }
}